package org.javaboy.ai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.javaboy.ai.domain.Category;
import org.javaboy.ai.domain.CategoryItem;
import org.javaboy.ai.mapper.CategoryItemMapper;
import org.javaboy.ai.service.ICategoryItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author javaboy
 * @since 2024-07-03
 */
@Service
public class CategoryItemServiceImpl extends ServiceImpl<CategoryItemMapper, CategoryItem> implements ICategoryItemService {

    @Autowired
    CategoryItemMapper categoryItemMapper;

    @Override
    public List<CategoryItem> getCategoryItems(String category) {
        LambdaQueryWrapper<CategoryItem> qw = new LambdaQueryWrapper<>();
        qw.eq(CategoryItem::getDelFlag, 1);
        qw.eq(CategoryItem::getCategory, category);
        qw.orderByAsc(CategoryItem::getOrderNum);
        qw.select(CategoryItem::getText, CategoryItem::getValue);
        return list(qw);
    }

    @Override
    public List<CategoryItem> selectCategoryItemList(CategoryItem categoryItem) {
        return categoryItemMapper.selectCategoryItemList(categoryItem);
    }

    @Override
    public boolean deleteCategoryItems(Long[] categoryItemIds) {
        LambdaUpdateWrapper<CategoryItem> uw = new LambdaUpdateWrapper<>();
        uw.in(CategoryItem::getId, categoryItemIds);
        uw.set(CategoryItem::getDelFlag, 0);
        return update(uw);
    }

    @Override
    public boolean checkValueAndTextExists(CategoryItem categoryItem) {
        LambdaQueryWrapper<CategoryItem> qw = new LambdaQueryWrapper<>();
        qw.eq(CategoryItem::getDelFlag, 1);
        if (categoryItem.getId() != null) {
            qw.ne(CategoryItem::getId, categoryItem.getId());
        }
        qw.eq(CategoryItem::getValue, categoryItem.getValue()).or().eq(CategoryItem::getText, categoryItem.getText());
        return exists(qw);
    }
}
